﻿using DbUpgrade.General;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DbUpgrade
{
    public partial class FormAddDbIp : Form
    {
        private bool _isAdd = false;
        private int _dbInfoId;
        private int _dbIpId;
        private DbInfo  _dbInfo = null;
        private DbIp _dbIp = null;

        /// <summary>
        /// ip数据库连接
        /// </summary>
        private DalIp _dalIp = null;
        public FormAddDbIp()
        {
            InitializeComponent();
        }

        public static DialogResult ShowAddDbIp(int dbInfoId, DalIp dalIp)
        {
            FormAddDbIp form = new FormAddDbIp();
            form._isAdd = true;
            form._dbInfoId = dbInfoId;
            form._dalIp = dalIp;
            return form.ShowDialog();
        }

        public static DialogResult ShowEditDbIp(int dbIpId, DalIp dalIp)
        {
            FormAddDbIp form = new FormAddDbIp();
            form._isAdd = false;
            form._dbIpId = dbIpId;
            form._dalIp = dalIp;
            return form.ShowDialog();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string chineseName = edtChineseName.Text.Trim();
            string ip = edtIp.Text.Trim();
            int port = 0;
            if (int.TryParse(edtPort.Text.Trim(), out int result))
            {
                port = result;
            }
            string dbName = edtDbName.Text.Trim();
            string userName = edtUserName.Text.Trim();
            string password = edtPassword.Text.Trim();
            if (string.IsNullOrEmpty(chineseName))
            {
                MessageBox.Show("请输入库中文名称");
            }
            else if (string.IsNullOrEmpty(ip))
            {
                MessageBox.Show("请输入Ip");
            }
            else if (port <= 0)
            {
                MessageBox.Show("请输入端口");
            }
            else if (string.IsNullOrEmpty(dbName))
            {
                MessageBox.Show("请输入数据库名称");
            }
            else if (string.IsNullOrEmpty(userName))
            {
                MessageBox.Show("请输入用户名");
            }
            else if (string.IsNullOrEmpty(password))
            {
                MessageBox.Show("请输入密码");
            }
            else
            {
                if (_isAdd)
                {
                    string sql = string.Format("insert into DbIp (DbId,Ip,Port,DbName,UserName,Password) values ({0},'{1}',{2},'{3}','{4}','{5}')", _dbInfo.Id, ip, port, dbName, userName, password);
                    int i = _dalIp.Dal.ExecNoQuery(sql);
                    if (i > 0)
                    {
                        MessageBox.Show("新增成功");
                        DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show("新增失败");
                    }
                }
                else
                {
                    string sql = $"update DbIp set Ip='{ip}',Port={port},DbName='{dbName}',UserName='{userName}',Password='{password}' where id={_dbIpId}";
                    int i = _dalIp.Dal.ExecNoQuery(sql);
                    if (i > 0)
                    {
                        MessageBox.Show("修改成功");
                        DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show("修改失败");
                    }
                }
            }
        }

        private void FormAddDbInfo_Load(object sender, EventArgs e)
        {
            edtCheckName.Enabled = false;
            edtChineseName.Enabled = false;
            if (!_isAdd)
            {
                RefreshDataEdit();
            }
            else
            {
                edtPort.Text = "3306";
                RefreshDataAdd();
            }
        }

        private void RefreshDataAdd()
        {
            Task.Run(() =>
            {
                string sql = $"select * from DbInfo where id = {_dbInfoId}";
                DataTable dt = App.DalData.ExecDataTable(sql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow dr = dt.Rows[0];
                    DbInfo ver = new DbInfo();
                    ver.Id = dr["Id"].ToInt32Ex();
                    ver.DbChineseName = dr["DbChineseName"].ToStringEx();
                    ver.DbKey = dr["DbKey"].ToStringEx();
                    ver.DbCheckName = dr["DbCheckName"].ToStringEx();
                    _dbInfo = ver;
                    this.BeginInvoke(new Action(() =>
                    {
                        edtChineseName.Text = _dbInfo.DbChineseName;
                        edtCheckName.Text = _dbInfo.DbCheckName;
                    }));
                }
            });
        }

        private void RefreshDataEdit()
        {
            Task.Run(() =>
            {
                string sql = $"select * from DbIp where id = {_dbIpId}";
                DataTable dt = _dalIp.Dal.ExecDataTable(sql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow dr = dt.Rows[0];
                    DbIp ver = new DbIp();
                    ver.Id = dr["Id"].ToInt32Ex();
                    ver.DbId = dr["DbId"].ToInt32Ex();
                    ver.Ip = dr["Ip"].ToStringEx();
                    ver.Port = dr["Port"].ToInt32Ex();
                    ver.DbName = dr["DbName"].ToStringEx();
                    ver.UserName = dr["UserName"].ToStringEx();
                    ver.Password = dr["Password"].ToStringEx();
                    _dbIp = ver;

                    string sql2 = $"select * from DbInfo where id = {_dbIp.DbId}";
                    DataTable dt2 = App.DalData.ExecDataTable(sql2);
                    if (dt2 != null && dt2.Rows.Count > 0)
                    {
                        DataRow dr2 = dt2.Rows[0];
                        DbInfo ver2 = new DbInfo();
                        ver2.Id = dr2["Id"].ToInt32Ex();
                        ver2.DbChineseName = dr2["DbChineseName"].ToStringEx();
                        ver2.DbKey = dr2["DbKey"].ToStringEx();
                        ver2.DbCheckName = dr2["DbCheckName"].ToStringEx();
                        _dbInfo = ver2;
                        this.BeginInvoke(new Action(() =>
                        {
                            edtChineseName.Text = _dbInfo.DbChineseName;
                            edtCheckName.Text = _dbInfo.DbCheckName;
                            edtIp.Text = _dbIp.Ip;
                            edtPort.Text = _dbIp.Port.ToString();
                            edtDbName.Text = _dbIp.DbName;
                            edtUserName.Text = _dbIp.UserName;
                            edtPassword.Text = _dbIp.Password;
                        }));
                    }
                }
            });
        }
    }
}
